package com.uatair.common.utils;

import java.security.MessageDigest;
import java.nio.charset.StandardCharsets;

public class SecureUtil {
    public static String md5(String str) {
        try {
            // 1. 获取 MD5 实例
            MessageDigest md = MessageDigest.getInstance("MD5");

            // 2. 计算哈希值
            byte[] hash = md.digest(str.getBytes(StandardCharsets.UTF_8));

            // 3. 转换为十六进制字符串
            StringBuilder sb = new StringBuilder(128);
            for (byte b : hash) {
                sb.append(String.format("%02x", b));
            }

            // 4. 返回大写结果
            return sb.toString().toUpperCase();
        } catch (Exception e) {
            throw new RuntimeException("MD5加密失败", e);
        }
    }
}